﻿# -*- coding:utf-8 -*-
from django.db import models
from django.core.files.storage import FileSystemStorage
from django.contrib.auth.models import User
import os
from django.conf import settings

#storage=fs,    /srv/www/django/homesite
#fs = FileSystemStorage(location='/srv/www/django/homesite/media/files')


class Category(models.Model):
    parent = models.ForeignKey('self', blank=True, null=True, verbose_name='Принадлежность')
    title = models.CharField(max_length=32, verbose_name='Название')
    title_code = models.CharField(max_length=32, verbose_name='Название для УРЛ')
    description = models.TextField(blank=True, verbose_name='Описание')
    files_count = models.IntegerField(verbose_name="Файлов в категории")
    date = models.DateTimeField(auto_now_add=True) 

    class Meta:
        ordering = ['title']

    def __unicode__(self):
        return self.title

   

class File(models.Model):
    title = models.CharField(max_length=64, verbose_name='Название')
    category = models.ForeignKey(Category, blank=True, null=True, verbose_name='Категория')
    name = models.FileField(upload_to='files', verbose_name='Файл')
    name_real = models.CharField(max_length=100, verbose_name='Имя файла')
    mimetype = models.CharField(max_length=64, verbose_name='Тип mime')
    description =  models.TextField(blank=True, verbose_name='Описание')
    date = models.DateTimeField(auto_now_add=True, verbose_name='Дата')
    user = models.ForeignKey(User, verbose_name="Пользователь")
    download_cnt = models.IntegerField(verbose_name="Кол-во скачиваний")
    
    class Meta:
        ordering = ['-date']

    def __unicode__(self):
        return self.name
        
    def is_exist(self):
        return os.path.exists(os.path.join(settings.MEDIA_ROOT, 'files', os.path.basename(self.name.path)))
    